const fs = require("fs");
const Handlebars = require("handlebars");
import UTILS from "../utils/utils";

var operators = {
  "==": function (l, r) {
    return l == r;
  },
  "===": function (l, r) {
    return l === r;
  },
  "!=": function (l, r) {
    return l != r;
  },
  "!==": function (l, r) {
    return l !== r;
  },
  "<": function (l, r) {
    return l < r;
  },
  ">": function (l, r) {
    return l > r;
  },
  "<=": function (l, r) {
    return l <= r;
  },
  ">=": function (l, r) {
    return l >= r;
  },
  "&&": function (l, r) {
    return l && r;
  },
  "||": function (l, r) {
    return l || r;
  },
};

Handlebars.registerHelper("ifcomp", function (v1, operator, v2, options) {
  if (arguments.length < 3) {
    throw new Error('Handlerbars Helper "compare" needs 2 parameters');
  }

  if (!operators[operator]) {
    throw new Error(
      'Handlerbars Helper "compare" doesn\'t know the operator ' + operator
    );
  }

  var result = operators[operator](v1, v2);

  if (result) {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
});

Handlebars.registerHelper("ifall", function (v1, operator, v2s, options) {
  if (arguments.length < 3) {
    throw new Error('Handlerbars Helper "compare" needs 2 parameters');
  }

  if (!operators[operator]) {
    throw new Error(
      'Handlerbars Helper "compare" doesn\'t know the operator ' + operator
    );
  }

  var result = true;

  var v2list = v2s.split(",");
  for (var i = 0; i < v2list.length; i++) {
    var v2 = v2list[i];
    result = result && operators[operator](v1, v2);
  }

  if (result) {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
});

Handlebars.registerHelper("ifany", function (v1, operator, v2s, options) {
  if (arguments.length < 3) {
    throw new Error('Handlerbars Helper "compare" needs 2 parameters');
  }

  if (!operators[operator]) {
    throw new Error(
      'Handlerbars Helper "compare" doesn\'t know the operator ' + operator
    );
  }

  var result = false;
  var v2list = v2s.split(",");
  for (var i = 0; i < v2list.length; i++) {
    var v2 = v2list[i];
    result = result || operators[operator](v1, v2);
  }

  if (result) {
    return options.fn(this);
  } else {
    return options.inverse(this);
  }
});

Handlebars.registerHelper("txt", function (str) {
  return str;
});

Handlebars.registerHelper("field", function (str) {
  return UTILS.toFieldCase(str);
});

Handlebars.registerHelper("camel", function (str) {
  return UTILS.toCamelCase(str);
});

Handlebars.registerHelper("lower", function (str) {
  return UTILS.toCamelCase(str).toLowerCase();
});

Handlebars.registerHelper("upper", function (str) {
  return UTILS.toCamelCase(str).toUpperCase();
});

Handlebars.registerHelper("slash", function (str) {
  return str.replaceAll("_", "/");
});

Handlebars.registerHelper("colon", function (str) {
  return str.replaceAll("_", ":");
});

Handlebars.registerHelper("title", function (str) {
  return str[0].toUpperCase() + str.substring(1);
});

Handlebars.registerHelper("raw", function (options) {
  return options.fn();
});

function compile(filePath, json) {
  //const fileUrl = new URL(filePath);

  let vm = fs.readFileSync(filePath).toString();

  const template = Handlebars.compile(vm);

  var html = template(json);

  return html;
}

let HandlebarsHelper = {
  compile,
};

export default HandlebarsHelper;
